import cv2
import time
import numpy as np

# user, pwd, ip, channel = "admin", "buaa0707", "192.168.1.103", 1 # 主码流
user, pwd, ip, channel = "admin", "buaa0707", "192.168.1.103", 2 # 子码流 分辨率低

video_stream_path = "rtsp://%s:%s@%s//Streaming/Channels/%d" % (user, pwd, ip, channel)  # HIKIVISION new version 2017
video = cv2.VideoCapture(video_stream_path)
l_used_time = []
fps = 0
while True:
    s = time.time()
    ret, frame = video.read()
    # print(ret)
    # image = cv2.resize(frame, (416, 416))
    # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    # cv2.imshow("results", image)
    cv2.putText(frame, text='FPS: {}'.format(fps), org=(3, 15), fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                fontScale=0.50, color=(255, 0, 0), thickness=2)
    cv2.imshow("results", frame)
    # cv2.imwrite("frame.jpg",frame)
    cv2.waitKey(1)
    used_time = time.time() - s
    l_used_time.append(used_time)
    if len(l_used_time) > 10:
        l_used_time.pop(0)
    fps = int(1 / np.mean(l_used_time))
